package com.znzz.controller;

import com.znzz.entity.Files;
import com.znzz.entity.User;
import com.znzz.service.FilesService;
import com.znzz.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(path = "/admin")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private FilesService filesService;


    @PostMapping(path = "/loginPage")
    public String loginVerify(User user, HttpSession session){
        User admin=userService.queryUser(user);

        if(admin!=null){
            session.setAttribute("admin",admin);
            return "admin/admin_index";
        }else{
            return "admin/admin_login";
        }
    }

    @RequestMapping(path = "/logout")
    public String logout(HttpSession session){

        session.invalidate();
        return "redirect:/admin_login.jsp";
    }


    @GetMapping(path = "/fileUpload")
    public String fileUpload(){

        return "admin/files_upload";
    }

    @GetMapping(path = "/queryFiles")
    public String queryFiles(@Param(value = "title") String title, Model model){
        List<Files> filesList=filesService.queryByUsername(title);
        model.addAttribute("filesList",filesList);

        return "showFiles";
    }

    @RequestMapping(path = "/editNews")
    public String editNews(){

        return "admin/edit_news";
    }


}
